package com.java.androidmarket.controller;

import com.java.androidmarket.dto.AddressDTO;
import com.java.androidmarket.entity.Address;
import com.java.androidmarket.entity.Result;
import com.java.androidmarket.service.AddressService;
import com.java.androidmarket.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * description:
 * author:张腾
 * date:2021-05-08
 */

@RestController
@CrossOrigin
@RequestMapping("address")
@Api(tags = "地址模块")
public class AddressController {

    @Autowired
    AddressService addressService;
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("get")
    @ApiOperation(value = "获取用户地址", notes = "根绝用户ID获取用户的地址信息")
    @ApiImplicitParam(dataType = "int", name = "userId", value = "用户ID")
    public Result getAddress(int userId){

        String key = "Address:" + userId;
        List<Address> addressList = new ArrayList<>();
        if (redisUtils.exists(key)){
            System.out.println("redis查找用户地址");
            Set<String> hmKeys = redisUtils.hmGetKeys(key);
            for (String hmKey : hmKeys) {
                addressList.add((Address) redisUtils.hmGet(key,hmKey));
            }

        }else{
            System.out.println("数据库查找用户地址");

            addressList = addressService.getAddress(userId);
            for (int i = 0; i < addressList.size(); i++) {
                redisUtils.hmSet(key,String.valueOf(i),addressList.get(i));
            }

        }

        return Result.success(addressList,addressList.size());
    }


    @GetMapping("default")
    @ApiOperation(value = "获取默认地址", notes = "获取用户默认地址")
    @ApiImplicitParam(dataType = "int",name = "userId", value = "用户ID")
    public Result getDefaultAdd(int userId){

        System.out.println("查找用户默认地址");
        Address defaultAdd = addressService.getDefaultAdd(userId);
        if (defaultAdd==null) {
            List<Address> addressList = addressService.getAddress(userId);
            if (addressList.size()>0) {
                defaultAdd = addressList.get(0);
            }else{
                return Result.success(null,0);
            }
        }
        return Result.success(defaultAdd,1);

    }


    @PostMapping("newAdd")
    public Result newAdd(@RequestBody Address address){

        if (redisUtils.exists("Address:"+address.getUserId())){
            redisUtils.del("Address:"+address.getUserId());
        }
        addressService.newAdd(address);

        return Result.success();
    }


    @PostMapping("updateAdd")
    @ApiOperation(value = "修改地址信息", notes = "修改用户收货地址信息")
    @ApiImplicitParam(dataType = "Address", name = "address", value = "地址实体类Address")
    public Result updateAdd(@RequestBody Address address){

        if (redisUtils.exists("Address:"+address.getUserId())){
            redisUtils.del("Address:"+address.getUserId());
        }
        addressService.updateAdd(address);

        return Result.success();
    }


    @PostMapping("delete")
    @ApiOperation(value = "删除地址", notes = "删除用户收货地址")
    @ApiImplicitParam(dataType = "int", name = "addressId", value = "地址表ID")
    public Result deleteAdd(@RequestBody AddressDTO addressDTO){

        int addressId = addressDTO.getAddressId();
        int userId = addressService.getUserId(addressId);

        if (redisUtils.exists("Address:"+userId)){
            redisUtils.del("Address:"+userId);
        }
        addressService.deleteAdd(addressId);

        return Result.success();
    }

    @PostMapping("updateState")
    public Result updateState(@RequestBody AddressDTO addressDTO) {

        if (redisUtils.exists("Address:"+addressDTO.getUserId())){
            redisUtils.del("Address:"+addressDTO.getUserId());
        }
        if (addressDTO.getAddressPreId()!=0){
            addressService.updateState(addressDTO.getAddressPreId(), 0);
        }
        addressService.updateState(addressDTO.getAddressCurId(), 1);

        return Result.success();
    }
}
